<!-- home -->
<template>
  <div class="login-box">
    <div class="title">欢迎登录</div>
    <van-form @submit="handleLogin">
      <van-cell-group inset>
        <van-field
          v-model="data.username"
          label="用户名"
          placeholder="请输入用户名"
          label-width="50px"
          :rules="[{ required: true, message: '' }]"
        />
        <van-field
          v-model="data.password"
          label="密码"
          type="password"
          placeholder="请输入密码"
          label-width="50px"
          :rules="[{ required: true, message: '' }]"
        />
      </van-cell-group>
      <div class="nouser">没有账号？<router-link :to="{ path: '/register' }">立即注册</router-link></div>
      <van-button round block type="info"  native-type="submit">立即登录</van-button>
    </van-form>
  </div>
</template>

<script>
import { login } from '@/api/common.js'
import encrypt from '@/utils/encrypt'
import { setToken,setUserId } from '@/utils/auth'
export default {
  data() {
    return {
     data:{
      username:'',
      password:'',
     },
    }
  },
  computed: {},
  mounted() {
  },
  created() {
  },
  methods: {
    async handleLogin() {
      const password = await encrypt(this.data.password)
      await login({...this.data,password}).then((res) => {
        if(res.code == 200){
          setToken(res.data.token)
          setUserId(res.data.info.id)
          this.$store.dispatch('setToken', res.data.token)
          this.$store.dispatch('setUserId', res.data.info.id)
          this.$notify({ type: 'success', message: res.message })
          this.$router.push({
            path:'/home'
          })
        }else{
          this.$notify(res.message)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.login-box{
  width: 100%;
  height: 100vh;
  background: url("../../../static/images/bg2.jpg") center center no-repeat;
  background-size: 100% 100%;
  .nouser{
    margin: .1rem 0.5rem;
  }
  .title{
    text-align: center;
    font-size: .7rem;
    padding-top: 20%;
    margin-bottom: 2.5rem;
    // font-weight: bold;
  }
  button{
    width: 80%;
    margin:1rem auto 0;
  }
}
</style>
